<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('INTERPHOTO')) die('File not found!');

class DB
{
  var $server   = "localhost";
  var $user     = "root";
  var $password = "";
  var $database = "";

  var $conn     = 0;
  var $queryid  = 0;
  var $row   = array();

  var $errdesc  = "";
  var $errno   = 0;
  
  var $query_count  = 0;


  // ###################### connect #######################
  function connect()
  {
    if(0 == $this->conn)
    {
      if($this->password=="")
      {
        $this->conn = mysqli_connect($this->server,$this->user);
      }
      else
      {
        $this->conn = mysqli_connect($this->server,$this->user,$this->password);
      }

       $sqlversion = @mysql_get_server_info();
	   if(empty($sqlversion)){$sqlversion='5.0';}
		
       if($sqlversion >= '4.1'){
           mysqli_query($this->conn,"set names 'utf8'");
           
       }

       if($sqlversion >= '5.0') {
           mysqli_query($this->conn,"SET sql_mode=''");
       }

      if(!$this->conn)
      {
        $this->error("Connection == false, connect failed");
      }

      if($this->database != "")
      {
        if(!mysqli_select_db( $this->conn,$this->database))
        {
          $this->error("cannot use database ".$this->database);
        }
      }
    }
  }



  // ###################### select database #######################
  function select_db($database = "")
  {
    if($database != "")
    {
      $this->database = $database;
    }

    if(!mysql_select_db($this->database, $this->conn))
    {
      $this->error("cannot use database ".$this->database);
    }
  }


  // ###################### query #######################
  function query($query_string)
  {
    $this->queryid = mysqli_query($this->conn,$query_string);

    if (!$this->queryid)
    {
      $this->error("Invalid SQL: ".$query_string);
    }
    $this->query_count++;

    return $this->queryid;
  }


  // ###################### query first #######################
  function query_first($query_string)
  {
    $queryid    = $this->query($query_string);
    $returnarray = $this->getrow($queryid, $query_string);

    $this->free_result($queryid);

    return $returnarray;
  }



  // ###################### fetch array #######################
  function getrow($queryid=-1,$query_string="")
  {
    if($queryid != -1)
    {
      $this->queryid=$queryid;
    }
	
    if(isset($this->queryid))
    {
      if(($this->row = mysqli_fetch_array($this->queryid)) === FALSE)
	  {
	    return null;
	  }
    }
    else
    {
      if(!empty($query_string))
      {
        $this->error("Invalid query id (".$this->queryid.") on query string: $query_string");
      }
      else
      {
        $this->error("Invalid query id: ".$this->queryid);
      }
    }

    return $this->row;
  }


  // ###################### GetRows #######################
  function getrows($queryid=-1)
  {
	$array = array();
	while($item = $this->getrow($queryid)){
		$array[] = $item;
	}

    return $array;
  }


  // ###################### fetch array #######################
  function affected_rows()
  {
        if($this->conn)
        {
                $result = @mysql_affected_rows($this->conn);
                return $result;
        }
        else
        {
                return false;
        }
  }



  // ###################### free result #######################
  function free_result($queryid=-1)
  {
    if($queryid != -1)
    {
      $this->queryid = $queryid;
    }
    return @mysql_free_result($this->queryid);
  }



  // ###################### number of rows #######################
  function get_num_rows()
  {
    return mysql_num_rows($this->queryid);
  }

  
  // ###################### number of fields #######################
  function get_num_fields()
  {
    return mysql_num_fields($this->queryid);
  }


  // ############ return last auto_increment number ##############
  function insert_id()
  {
    return mysqli_insert_id($this->conn);
  }



  // ###################### close the connection to the database #######################
  function close()
  {
    return mysqli_close($this->conn);
  }



  // ###################### get error description #######################
  function geterrdesc()
  {
    $this->error = mysql_error();
    return $this->error;
  }



  // ###################### get error number #######################
  function geterrno()
  {
    $this->errno = mysql_errno();
    return $this->errno;
  }


  // ###################### error message #######################
  function error($msg)
  {
	$version = 'V'. APP_VERSION .' (UTF8)';

    $this->errdesc = mysql_error();
    $this->errno   = mysql_errno();

    $message  = "Database Query Error in InterPhoto " . $version . "\r\n\r\n";
    $message .= $msg."\r\n";
    $message .= "Error: ".$this->errdesc."\r\n";
    $message .= "Error number: ".$this->errno."\r\n";
    $message .= "Date: ".gmdate('Y-m-d H:i:s', time() + (3600 * 8)). "\r\n";
    $message .= "File: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

    echo '<center><br /><br /><br /><br /><b>InterPhoto Database Query Error Information:</b><br />
          <p><form><textarea rows="22" cols="60">'.htmlspecialchars($message).'</textarea></form></p></center>';

    exit();
  }

}

?>